package com.itheima.health.mq;


import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.SmsUtils;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class SmsSendMessage {

    @RabbitListener(queues = "heimait")
    public void receive(Map map, Channel channel, Message message) throws IOException {


        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            String telephone = (String) map.get("telephone");
            String code = (String) map.get("code");
            //SmsUtils.validUserTelephone(telephone, code);
            RedisUtil.set(RedisMessageConstant.SENDTYPE_ORDER + telephone, code, 60, TimeUnit.SECONDS);
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, true);
        }
    }
}
